package cn.licoy.wdog.core.service.hospital.impl;

import cn.licoy.wdog.common.exception.RequestException;
import cn.licoy.wdog.core.dto.hospitalUser.AppversionDto;
import cn.licoy.wdog.core.dto.hospitalUser.PageUtileDto;
import cn.licoy.wdog.core.entity.hospital.AppVersion;
import cn.licoy.wdog.core.mapper.hospital.AppVersionMapper;
import cn.licoy.wdog.core.service.hospital.AppVersionService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author Sunne
 * Create time 2018/6/26 18:29
 * @return null
 **/
@Service
@Transactional
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper,AppVersion> implements AppVersionService {
    @Override
    public void save(AppversionDto appversionDto) {
        if (appversionDto!=null){
            if (appversionDto.getContent()==null||"".equals(appversionDto.getContent())){
                throw RequestException.fail("版本更新内容不能为空");
            }
            if (appversionDto.getFileName()==null||"".equals(appversionDto.getFileName())){
                throw RequestException.fail("文件名不能为空");
            }
            if (appversionDto.getVersion()==null||"".equals(appversionDto.getVersion())){
                throw RequestException.fail("版本号不能为空");
            }
            AppVersion appVersion = new AppVersion();

            appVersion.setContent(appversionDto.getContent());
            appVersion.setFileName(appversionDto.getFileName());
            appVersion.setVersion(appversionDto.getVersion());
            appVersion.setFileSize(appversionDto.getFileSize());
            appVersion.setUrl("http://120.76.205.151:1400/appversion/download?filename="+appversionDto.getFileName());
            appVersion.setCreateDate(new Date());
            appVersion.setRemake("1");
            try {
                this.insert(appVersion);
            }catch (Exception e){
                throw RequestException.fail("app信息保存失败");
            }

        }else {
            throw RequestException.fail("app信息不能为空");
        }

    }

    @Override
    public AppVersion latestversion() {
        EntityWrapper<AppVersion> wrapper = new EntityWrapper<>();
        wrapper.orderBy("create_date",false);
        Page<AppVersion> Page = this.selectPage(new Page<>(1,
                10), wrapper);
        if (Page.getRecords().size()>0){
            AppVersion appVersion = Page.getRecords().get(0);
            return appVersion;
        }else {
            throw RequestException.fail("没有最新版本了");
        }

    }

    @Override
    public Page<AppVersion> selectAll(PageUtileDto dto) {
        if (dto.getPage()==null||"".equals(dto.getPage())){
            dto.setPage(1);
        }
        if (dto.getPageSize()==null||"".equals(dto.getPageSize())){
            dto.setPageSize(20);
        }
        EntityWrapper<AppVersion> wrapper = new EntityWrapper<>();
        wrapper.orderBy("id",false);
        Page<AppVersion> Page = this.selectPage(new Page<>(dto.getPage(),
                dto.getPageSize()), wrapper);
        return Page;

    }
}
